package com.lanou3g.ptp.message.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.lanou3g.ptp.entity.Message;
import com.lanou3g.ptp.message.mapper.MessageMapper;
import com.lanou3g.ptp.message.service.MessageService;
import com.lanou3g.ptp.vo.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service(timeout = 5000)
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public boolean saveSignMessage(Message message) {
        message.setType(Message.TYPE_PRACTICE);
        message.setTitle("签到提醒");
        return saveMessage(message);
    }

    @Override
    public boolean saveDiaryMessage(Message message) {
        message.setType(Message.TYPE_PRACTICE);
        message.setTitle("日志提醒");
        return saveMessage(message);
    }

    @Override
    public boolean saveReportMessage(Message message) {
        message.setType(Message.TYPE_PRACTICE);
        message.setTitle("阶段报告提醒");
        return saveMessage(message);
    }

    @Override
    public boolean saveSummaryMessage(Message message) {
        message.setType(Message.TYPE_PRACTICE);
        message.setTitle("总结提醒");
        return saveMessage(message);
    }

    @Override
    public boolean saveApprovalMessage(Message message) {
        message.setType(Message.TYPE_APPROVAL);
        message.setTitle("审核提醒");
        return saveMessage(message);
    }

    @Override
    public boolean saveTempSignMessage(Message message) {
        message.setType(Message.TYPE_TEMP_SIGN);
        message.setTitle("查岗提醒");
        return saveMessage(message);
    }

    @Override
    public boolean readMessageByStudentIdAndType(Long studentId, Integer type) {
        return messageMapper.readMessageByStudentIdAndType(studentId, type) > 0;
    }

    @Override
    public MessageResult findNotReadMsgByStudentId(Long studentId) {
        List<Message> messages = messageMapper.findNotReadMsgByStudentId(studentId);
        MessageResult messageResult = new MessageResult();
        List<Message> practiceMessage = messageResult.getPracticeMessage();
        List<Message> approvalMessage = messageResult.getApprovalMessage();
        List<Message> tempSignMessage = messageResult.getTempSignMessage();
        for (Message message : messages) {
            Integer type = message.getType();
            if (Message.TYPE_PRACTICE.equals(type)) {
                practiceMessage.add(message);
            }
            if (Message.TYPE_APPROVAL.equals(type)) {
                approvalMessage.add(message);
            }
            if (Message.TYPE_TEMP_SIGN.equals(type)) {
                tempSignMessage.add(message);
            }
        }
        messageResult.setCount(practiceMessage.size() + approvalMessage.size() + tempSignMessage.size());
        return messageResult;
    }

    private boolean saveMessage(Message message) {
        Date now = new Date();
        message.setCreateTime(now);
        message.setUpdateTime(now);
        message.setStatus(Message.STATUS_NOT_READ);
        return messageMapper.insert(message) > 0;
    }
}
